package com.wd.live.module_base.net;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;



import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class HttpUtils1 {
    private final ApiService apiService;
    private Retrofit retrofit;
    public ApiService getApiService(){
        return apiService;
    }
    private HttpUtils1(){
        //实例化 okHttp
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                //设置连接和读取时间
                .connectTimeout(5, TimeUnit.SECONDS)
                .readTimeout(5, TimeUnit.SECONDS)
                //网络日志拦截
                .addInterceptor(new HttpLoggingInterceptor()
                        .setLevel(HttpLoggingInterceptor.Level.BODY))
                .build();
        //实例化 retrofit
        retrofit = new Retrofit.Builder()
                //设置类型转换
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .baseUrl("http://10.59.9.18/")
                .build();
        //动态代理
        apiService = retrofit.create(ApiService.class);
    }
    public <T> T create(final Class<T> service) {
        return retrofit.create(service);
    }
    private static HttpUtils1 httpUtils;
    public static HttpUtils1 getHttpUtils() {
        if (httpUtils == null) {
            synchronized (HttpUtils1.class) {
                if (httpUtils == null) {
                    httpUtils = new HttpUtils1();
                }
            }
        }
        return httpUtils;
    }
    /**
     * 判断是否有WIFI网络
     */
    public boolean hasNet(Context context) {
        ConnectivityManager systemService = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = systemService.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        return networkInfo.isConnected() && networkInfo != null;

    }
}
